﻿Imports VendasDAL
Public Class VendaBLL
    Dim ObjDAL As VendaDAL

    Property VenCodigo As Integer
    Property ProCodigo As Integer
    Property Data As Nullable(Of DateTime)
    Property Qtde As Integer
    Property ValorUnitario As Decimal
    Property Desconto As Nullable(Of Decimal)
    Property Total As Nullable(Of Decimal)
    Property CodFuncionario As Integer
    Property CodFuncionario2 As Integer
    Property CodCliente As Integer
    Property CodStatus As Integer
    Property Obs As String


#Region "Construtor"
    Sub New()

    End Sub

    Sub New(codigo As Integer)
        carrega(codigo)
    End Sub

    Sub New(codigo As Integer, procodigo As Integer)
        Me.VenCodigo = codigo
        Me.ProCodigo = procodigo
        Me.Qtde = 0
        Me.Desconto = 0.0
        Me.Total = 0.0
        Me.CodStatus = 0
        Me.ValorUnitario = 0.0
    End Sub

    Sub New(procodigo As Integer, qtde As Integer, vlunitario As Decimal, desconto As Decimal, vencodigo As Integer, total As Decimal, status As Integer)
        Me.VenCodigo = vencodigo
        Me.ValorUnitario = vlunitario
        Me.Desconto = desconto
        Me.Qtde = qtde
        Me.Desconto = desconto
        Me.ProCodigo = procodigo
        Me.Total = total
        Me.CodStatus = status
    End Sub

    Sub New(Codigo As Integer, CliCodigo As Integer, CodStatus As Integer, CodFuncionario As Integer, Data As Nullable(Of DateTime), obs As String)
        Me.VenCodigo = Codigo
        Me.CodCliente = CliCodigo
        Me.CodStatus = CodStatus
        Me.CodFuncionario = CodFuncionario
        Me.Data = Data
        Me.Obs = obs
    End Sub
#End Region

#Region "Métodos"
    Public Function RetItens(ColCodigo As Integer) As DataTable
        Try
            ObjDAL = New VendaDAL
            Return ObjDAL.RetItens(ColCodigo)
        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Sub RetItem()
        Dim tab As DataTable
        Try
            ObjDAL = New VendaDAL
            tab = ObjDAL.RetItem(VenCodigo, ProCodigo)
            If (tab.Rows.Count > 0) Then
                Me.ProCodigo = Integer.Parse(tab.Rows(0)("pro_codigo").ToString)
                Me.Qtde = Integer.Parse(tab.Rows(0)("itv_qtde").ToString)
                Me.ValorUnitario = Decimal.Parse(tab.Rows(0)("itv_valorunitario").ToString)
                Me.Desconto = tab.Rows(0)("itv_desconto").ToString
                Me.CodStatus = tab.Rows(0)("sta_codigo").ToString
            End If

        Catch ex As Exception
            Throw
        End Try
    End Sub

    Public Function IAEVenda(Operacao As Char) As Integer
        Try
            ObjDAL = New VendaDAL
            Return ObjDAL.IAEVenda(VenCodigo, Data, CodFuncionario, CodCliente, CodStatus, Operacao, Obs)
        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Function InsereOrdemServico(Codigo As Integer) As Integer
        Try
            ObjDAL = New VendaDAL
            Return ObjDAL.InsereOrdemServico(Codigo)
        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Sub IAEItens(Operacao As Char)
        Try
            ObjDAL = New VendaDAL
            ObjDAL.IAEItens(VenCodigo, ProCodigo, Qtde, ValorUnitario, Desconto, Total, CodStatus, Operacao)
        Catch ex As Exception
            Throw
        End Try
    End Sub

#End Region

#Region "Buscar"
    Function buscar(ByVal descricao As String, tipo As Integer) As DataTable
        Dim ret As DataTable
        Try
            ObjDAL = New VendaDAL
            ret = ObjDAL.buscar(descricao, tipo)
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        Return ret
    End Function

    Function locdata(ByVal dataIni As DateTime, ByRef datafim As DateTime) As DataTable 'buscar datas cbfiltro
        Dim ret As DataTable
        Try
            ObjDAL = New VendaDAL
            If dataIni <= datafim Then
                ret = ObjDAL.locadata(dataIni, datafim)
            Else : Throw New Exception("Data inical maior que data final")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        Return ret
    End Function
#End Region

#Region "Carrega Propriedades"
    Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            ObjDAL = New VendaDAL
            tab = ObjDAL.buscar(codigo, 0)
            If (tab.Rows.Count > 0) Then
                Me.CodFuncionario = Integer.Parse(tab.Rows(0)("fun_codigo").ToString)
                Me.CodCliente = tab.Rows(0)("cli_codigo").ToString
                Me.VenCodigo = tab.Rows(0)("ven_codigo").ToString
                If (tab.Rows(0)("ven_data") Is DBNull.Value) Then : Me.Data = Nothing
                Else : Me.Data = tab.Rows(0)("ven_data").ToString
                End If
                If (tab.Rows(0)("ven_total") Is DBNull.Value) Then : Me.Total = Nothing
                Else : Me.Total = tab.Rows(0)("ven_total").ToString
                End If
                Me.CodStatus = tab.Rows(0)("sta_codigo").ToString
                Me.Obs = tab.Rows(0)("ven_obs").ToString
            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
    End Sub
#End Region

    Sub Limpar()
        Me.CodFuncionario = Nothing
        Me.CodCliente = Nothing
        Me.VenCodigo = Nothing
        Me.Data = Nothing
        Me.CodStatus = Nothing
        Me.Total = Nothing
    End Sub
End Class
